package br.ufc.DAO.Implements;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.ufc.DAO.IAlunoDao;
import br.ufc.si.model.Aluno;
import br.ufc.util.HibernateUtil;

public class AlunoDAO implements IAlunoDao {

	public void save(Aluno aluno) {
		Session sessao = HibernateUtil.getSession();

		Transaction txt = sessao.beginTransaction();

		try {
			sessao.save(aluno);
			txt.commit();
		} catch (Exception e) {
			txt.rollback();
		} finally {
			sessao.close();
		}

	}

	public void update(Aluno a) {
		Session sessao = HibernateUtil.getSession();
		Transaction trasaction = sessao.beginTransaction();
		try {
			sessao.update(a);
			trasaction.commit();

		} catch (Exception e) {
			trasaction.rollback();
		} finally {
			sessao.close();
		}
	}

	public void delete(Aluno aluno) {

		Session sessao = HibernateUtil.getSession();
		Transaction txt = sessao.beginTransaction();

		try {
			sessao.delete(aluno);
			txt.commit();

		} catch (Exception e) {
			txt.rollback();
		} finally {
			sessao.close();
		}
	}

	public List<Aluno> listALL() {
		Session session = HibernateUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Aluno.class);
			return criteria.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;

	}
}
